﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.FoundationModel;

namespace MINERP.InfoConfig
{
    public partial class FrmShippingManager : Form
    {
        public FrmShippingManager()
        {
            InitializeComponent();
        }

        private static FrmShippingManager _instance;
        public static FrmShippingManager CreateInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmShippingManager();
                }
                return _instance;
            }
        }
        private void FrmShippingManager_Load(object sender, EventArgs e)
        {
            SetFormEnable(false);
            btnAddNew.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            simpleButtonSave.Enabled = false;
            btnCancel.Enabled = false;
            List<ShippingCompany_Info> list = _bll.GetAllShippingCompany();
            gridControlShippingCompany.DataSource = list;
        }


        void SetFormEnable(bool inBool)
        {
            textEditName.Enabled = inBool;
            textEditAlias.Enabled = inBool;
            textEditNetAddr.Enabled = inBool;
            textEditPrintFormat.Enabled = inBool;
        }

        private int OperInt = -1;
        ShippingBLL _bll=new ShippingBLL();
        private void btnAddNewShop_Click(object sender, EventArgs e)
        {
            SetFormEnable(true);
            textEditName.Text = "";
            textEditAlias.Text = "";
            textEditNetAddr.Text = "";
            textEditPrintFormat.Text = "";
            OperInt = 0;

            btnAddNew.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            simpleButtonSave.Enabled = true;
            btnCancel.Enabled = true;

        }

        private void btnUpdateShop_Click(object sender, EventArgs e)
        {
            SetFormEnable(true);
            OperInt=  1;

            btnAddNew.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            simpleButtonSave.Enabled = true;
            btnCancel.Enabled = true;
        }
        
        private void simpleButtonSave_Click(object sender, EventArgs e)
        {
            btnAddNew.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            simpleButtonSave.Enabled = false;
            btnCancel.Enabled = false;
            if ((OperInt == 0 ) && CheckData())//添加模式
            {
                ShippingCompany_Info companyInfo=new ShippingCompany_Info();
                companyInfo.ShippingCompanyName = textEditName.Text.Trim();
                companyInfo.ShippingFormatPath = textEditPrintFormat.Text.Trim();
                companyInfo.TrackingUrl = textEditNetAddr.Text.Trim();
                companyInfo.Alias = textEditAlias.Text.Trim();

               
                _bll.AddOrUpdateShipping(companyInfo);
            }
            else if (( OperInt == 1) && CheckData())//添加模式
            {
                ShippingCompany_Info companyInfo = new ShippingCompany_Info();
                companyInfo.ShippingCompanyName = textEditName.Text.Trim();
                companyInfo.ShippingFormatPath = textEditPrintFormat.Text.Trim();
                companyInfo.TrackingUrl = textEditNetAddr.Text.Trim();
                companyInfo.Alias = textEditAlias.Text.Trim();

                companyInfo.ShippingCompanyID = Convert.ToInt32(textEditCompanyId.Text.Trim());
                _bll.AddOrUpdateShipping(companyInfo);
            }
            
            else
            {
               
            }
            FrmShippingManager_Load(null,null);
        }
        bool CheckData()
        {
            if(string.IsNullOrEmpty(textEditName.Text.Trim()))
            {
                MessageBox.Show("公司全称不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            if (string.IsNullOrEmpty(textEditAlias.Text.Trim()))
            {
                MessageBox.Show("别名不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            if (string.IsNullOrEmpty(textEditNetAddr.Text.Trim()))
            {
              
                MessageBox.Show("查询网址不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            else
            {

               // string url = textEditNetAddr.Text.Trim();
               //if(url.Contains(@"http://")&&url.Contains("{0}"))
               //{

               //}
               //else
               //{
               //    MessageBox.Show("网站地址不符合规则应包含开头'http://'和'{0}'", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
               //    return false;
               //}


            }
            if (string.IsNullOrEmpty(textEditPrintFormat.Text.Trim()))
            {
                MessageBox.Show("打印格式文件不能为空", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            return true;

        }

        private void btnDeleteShop_Click(object sender, EventArgs e)
        {
           
                ShippingCompany_Info info = (ShippingCompany_Info)gridViewShippingCompany.GetFocusedRow();

                if(MessageBox.Show("确定要删除 "+info.ShippingCompanyName+"?","确定",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
                {
                    _bll.DelShippingCompany(info);
                }
                btnAddNew.Enabled = true;
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
                simpleButtonSave.Enabled = false;
                btnCancel.Enabled = false;
                FrmShippingManager_Load(null, null);
        }

        private void gridViewShippingCompany_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if(e.RowHandle>=0)
            //{
            //    ShippingCompany_Info info = (ShippingCompany_Info) gridViewShippingCompany.GetFocusedRow();
            //    textEditName.Text = info.ShippingCompanyName;
            //    textEditAlias.Text = info.Alias;
            //    textEditNetAddr.Text = info.TrackingUrl;
            //    textEditPrintFormat.Text = info.ShippingFormatPath;
            //}
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAddNew.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            simpleButtonSave.Enabled = false;
            btnCancel.Enabled = false;
        }

        private void FrmShippingManager_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void gridViewShippingCompany_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                ShippingCompany_Info info = (ShippingCompany_Info)gridViewShippingCompany.GetFocusedRow();
                textEditName.Text = info.ShippingCompanyName;
                textEditAlias.Text = info.Alias;
                textEditNetAddr.Text = info.TrackingUrl;
                textEditPrintFormat.Text = info.ShippingFormatPath;
                textEditCompanyId.Text = info.ShippingCompanyID.ToString();
            }
        }

        private void simpleButtonExport_Click(object sender, EventArgs e)
        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            //saveFileDialog.AddExtension = true;
            saveFileDialog.FilterIndex = 3;
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = saveFileDialog.FileName;
                if (saveFileDialog.OpenFile() != null)
                {
                    switch (saveFileDialog.FilterIndex)
                    {
                        case 3:
                            gridViewShippingCompany.ExportToXls(fileName);
                            return;
                        case 4:
                            gridViewShippingCompany.ExportToHtml(fileName);
                            return;
                        case 2:
                            gridViewShippingCompany.ExportToPdf(fileName);
                            return;
                        case 1:
                            this.gridViewShippingCompany.ExportToText(fileName);
                            return;
                        default:
                            break;
                    }
                }

            }

        }

        
    }
}
